package com.yayan.beautyclub.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.yayan.beautyclub.bean.Admin;
import com.yayan.beautyclub.service.IAdminService;
import com.yayan.util.EncryptUtil;

@Controller
@RequestMapping(value="/admin")
@SessionAttributes("admin")
public class AdminController {

	@Autowired
	private IAdminService adminService;
	
	/**
	 * 后台管理，管理员登录检查
	 * @param userName
	 * @param password
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String login(String userName, String password, Model model){	
		//System.out.println(EncryptUtil.EncoderByMd5(password));
		Admin admin = adminService.getAdmin(userName, password);
		if(admin == null){
			admin = new Admin();
			admin.setaName(userName);
			admin.setaPwd(password);
			admin.setRemarks("账户或密码错误！");
			admin.setStatus(0);
			model.addAttribute("admin", admin);			
			return "redirect:/index.jsp";
		}		
		model.addAttribute("admin", admin);		
		return "redirect:home";
	}
	
	/**
	 * 后台管理主页
	 * @return
	 */
	@RequestMapping(value="/home")
	public String homePage(){		
		return "home";
	}
	
	/**
	 * 后台管理，获取预约列表页面
	 * @return
	 */
	@RequestMapping(value="/reserve", method=RequestMethod.GET)
	public String reservePage(){	
		return "reserve";
	}
	
	/**
	 * 后台管理，获取产品列表页面
	 * @return
	 */
	@RequestMapping(value="/product_list", method=RequestMethod.GET)
	public String productListPage(){
		return "product_list";
	}
	
	/**
	 * 后台管理，产品录入页面
	 * @return
	 */
	@RequestMapping(value="/product_input", method=RequestMethod.GET)
	public String productInputPage(){
		return "product_input";
	}
	
	/**
	 * 后台管理，类别信息页面
	 * @return
	 */
	@RequestMapping(value="/category", method=RequestMethod.GET)
	public String categoryPage(){
		return "category";
	}
	
	/**
	 * 后台管理，优惠券页面
	 * @return
	 */
	@RequestMapping(value="/ticket", method=RequestMethod.GET)
	public String ticketPage(){
		return "ticket";
	}
	
	/**
	 * 后台管理注销登录
	 * @param status
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String logout(SessionStatus status, HttpSession httpSession){		
		httpSession.removeAttribute("admin");
		status.setComplete();		
		return "redirect:/index.jsp";
	}
	
	
	@RequestMapping(value="/get_admin.do", method=RequestMethod.POST)
	@ResponseBody
	public Admin getAdmin(@RequestBody Map<String, String> map){
		String name = map.get("name");
		String pwd = map.get("pwd");
		return adminService.getAdmin(name, pwd);
	}
	
	@RequestMapping(value="/create.do", method=RequestMethod.POST)
	@ResponseBody
	public boolean createAdmin(@RequestBody Admin admin){
		if(adminService.save(admin) == 0L)
			return false;
		return true;
	}
	
	@RequestMapping(value="/update_pwd.do", method=RequestMethod.POST)
	@ResponseBody
	public boolean updatePwd(@RequestBody Map<String, String> map){
		String name = map.get("name");
		String pwd = map.get("pwd");
		return adminService.setPwd(name, pwd);
	}
	
	@RequestMapping(value="/update_authority.do", method=RequestMethod.POST)
	@ResponseBody
	public boolean updateAuthority(@RequestBody Map<String, String> map){
		String name = map.get("name");
		int authority = Integer.parseInt(map.get("authority"));
		return adminService.setAuthority(name, authority);
	}
	
	@RequestMapping(value="/delete_admin.do", method=RequestMethod.POST)
	@ResponseBody
	public boolean deleteAdmin(@RequestBody Map<String, String> map){
		String name = map.get("name");
		return adminService.delete(name);
	}
}
